/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * usuario_alterar.java
 *
 * Created on 29/03/2010, 13:00:32
 */

package siga.visao;

import java.sql.SQLException;
import javax.swing.JOptionPane;
import siga.banco.conexao;
import siga.criptografia.md5;

/**
 *
 * @author rafaelfrasson
 */
public class usuario_alterar extends javax.swing.JDialog {

    /** Creates new form usuario_alterar */
    public usuario_alterar(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jtfSnOld = new javax.swing.JPasswordField();
        jLabel2 = new javax.swing.JLabel();
        jtfSnNew = new javax.swing.JPasswordField();
        jLabel3 = new javax.swing.JLabel();
        jtfSnNewR = new javax.swing.JPasswordField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Alterar senha de Usuário");

        jLabel1.setText("Senha Atual:");
        jLabel1.setName("jLabel1"); // NOI18N

        jtfSnOld.setName("jtfSnOld"); // NOI18N

        jLabel2.setText("Nova Senha:");
        jLabel2.setName("jLabel2"); // NOI18N

        jtfSnNew.setName("jtfSnNew"); // NOI18N

        jLabel3.setText("Repita Nova Senha:");
        jLabel3.setName("jLabel3"); // NOI18N

        jtfSnNewR.setName("jtfSnNewR"); // NOI18N

        jButton1.setText("Cancela");
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Confirma");
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jtfSnOld, javax.swing.GroupLayout.DEFAULT_SIZE, 165, Short.MAX_VALUE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jtfSnNew)
                    .addComponent(jLabel3)
                    .addComponent(jtfSnNewR))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(38, Short.MAX_VALUE)
                .addComponent(jButton2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jtfSnOld, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jtfSnNew, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jtfSnNewR, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        try {
            conexao.Query("select codigo from usuarios where codigo=" + index.usuario[0] + " and cod_emp=" + index.cod_emp + " and senha='" + md5.crpMD5(jtfSnOld.getText()) + "'");
            conexao.rs.next();
            if(conexao.rs.getString("codigo").equals(index.usuario[0].toString())){
                if(jtfSnNew.getText().equals(jtfSnNewR.getText())){
                    conexao.Inserir("update usuarios set senha='" + md5.crpMD5(jtfSnNew.getText()) + "' where codigo=" + index.usuario[0] + " and cod_emp=" + index.cod_emp);
                    JOptionPane.showMessageDialog(null, "Senha Alterada com Sucesso.","Informação do Sistema",JOptionPane.INFORMATION_MESSAGE);
                    dispose();
                }else{
                    JOptionPane.showMessageDialog(null, "Senhas incompatíveis.","Informação do Sistema",JOptionPane.ERROR_MESSAGE);
                }
            }else{
                JOptionPane.showMessageDialog(null, "Senhas atual inválida.","Informação do Sistema",JOptionPane.ERROR_MESSAGE);
            }
        } catch (SQLException ex) {
            verErros.setErro(ex.getMessage());
        }
    }//GEN-LAST:event_jButton2ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPasswordField jtfSnNew;
    private javax.swing.JPasswordField jtfSnNewR;
    private javax.swing.JPasswordField jtfSnOld;
    // End of variables declaration//GEN-END:variables

}
